import FileSaver from "file-saver";
import XLSX from "xlsx";
/**
 * 前端导出表格可以用这个 minxins 混入 
 * table 设置了fixed / 操作栏 导出都会有问题  可以传入控制她的字段
 */
const exportExcel = {
    computed:{
        tableHeadeInfo(){
            let obj = {}
            this.headerList.forEach((item)=>{
              if(item.exportProp){
                obj[item.exportProp] =  item.label
              }else{
                obj[item.prop] =  item.label
              }
            })
            return obj
        }
    },
    data(){
        return{
            exportLoading:false,
        }
    },
    methods: {
        //导出全部数据
        exportExcel2(obj) {    //table 导出
            this.exportLoading = true
            let {name,fn,i,tableId='#out-table'} = obj
            let options = {}
            if(i === 2) options.allPage = 2
            else {
                options = {...this.pageparm,allPage:2}
                delete options.page
                delete options.perPage
            }
            fn(options,{
                timeout: 10 * 60 * 1000,
            }).then(({ data }) => {
                let thead = '<thead><tr>'
                for(let key in this.tableHeadeInfo){
                    thead +=  `<td>${this.tableHeadeInfo[key]}</td>`
                }
                thead += '</tr></thead>'
                let tbody = '<tbody>'
                let tabStr = "";
                const tableHeadeInfo = Object.keys(this.tableHeadeInfo)
                data.forEach((item, index) => {
                    let tabRowStr = "";
                    tabStr += `<tr>`;
                    for (let i = 0; i < tableHeadeInfo.length; i++ ) {
                        const key = tableHeadeInfo[i]
                        if (/\./g.test(key)) {
                            let arr = key.split('.')
                            tabRowStr += `<td>${item[arr[0]] ? item[arr[0]][arr[1]] : ''}</td>`;
                        } else {
                            tabRowStr += `<td>${item[key] === null  ? '' : item[key]}</td>`;
                        }
                    }
                    tabStr += tabRowStr;
                    tabStr += "</tr>";
                });
                tbody += tabStr;
                tbody += '</tbody>'
                let tableStr = thead + tbody
                let table = document.createElement("table")
                table.innerHTML = tableStr
                let wb = XLSX.utils.table_to_book(table);
                /* 获取二进制字符串作为输出 */
                let wbout = XLSX.write(wb, {
                    bookType: "xlsx",
                    bookSST: false,
                    type: "array"
                });
                try {
                    FileSaver.saveAs(
                        new Blob([wbout], { type: "application/octet-stream" }),
                        //设置导出文件名称
                        `${name}.xlsx`
                    );
                } catch (e) {
                    if (typeof console !== "undefined") console.log(e, wbout);
                }
                this.exportLoading = false
                return wbout;
            }).catch((err)=>{
                console.log('err',err)
                this.exportLoading = false
                this.$message({
                    type: 'error',
                    message: '导出失败！'
                  });
            })
        },
        // exportExcel2(obj) {    //JSON 导出 这个更快但是对数据不能进行处理
        //     this.exportLoading = true
        //     let {name,fn,i,} = obj
        //     let options = {}
        //     if(i === 2) options.allPage = 2
        //     else {
        //         options = {...this.pageparm,allPage:2}
        //         delete options.page
        //         delete options.perPage
        //     }
        //     fn(options,{
        //         timeout: 10 * 60 * 1000,
        //     }).then(({ data }) => {
        //         data.unshift(this.tableHeadeInfo)
        //         let  worksheet = XLSX.utils.json_to_sheet(data, {skipHeader:true});
        //         const wb = XLSX.utils.book_new();
        //         XLSX.utils.book_append_sheet(wb, worksheet, "sheet1");
        //         let wbout = XLSX.write(wb, {
        //             bookType: "xlsx",
        //             bookSST: false,
        //             type: "array"
        //         });
        //         try {
        //             FileSaver.saveAs(
        //                 new Blob([wbout], { type: "application/octet-stream" }),
        //                 //设置导出文件名称
        //                 `${name}.xlsx`
        //             );
        //         } catch (e) {
        //             if (typeof console !== "undefined") console.log(e, wbout);
        //         }
        //         this.exportLoading = false
        //         return wbout;
        //     }).catch((err)=>{
        //         console.log('err',err)
        //         this.exportLoading = false
        //         this.$message({
        //             type: 'error',
        //             message: '导出失败！'
        //           });
        //     })
        // },
    }
}

export default exportExcel